﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewBag.Title = "题目管理";
    List<string> list = new List<string> { "A", "B", "C", "D" };

    Quest quest = ViewBag.quest;
}
<style>
    #btnAnswers button[answer=true] {
        background-color: #FF5722;
    }
</style>
<div>
    <a href="@Url.Action("QuestLib")" class="layui-btn">题目库</a>
</div>
<input id="txtID" type="hidden" value="@quest.ID" />
<div class="layui-form" style="margin:1em; padding:1em;">
    <div class="layui-form-item">
        <label class="layui-form-label">题目</label>
        <div class="layui-input-block">
            <input id="txtTitle" type="text" value="@quest.Title" required lay-verify="required" placeholder="请输入题目" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input id="txtOptionA" type="text" value="@quest.OptionA" required lay-verify="required" placeholder="请输入选项A" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input id="txtOptionB" type="text" value="@quest.OptionB" required lay-verify="required" placeholder="请输入选项B" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input id="txtOptionC" type="text" value="@quest.OptionC" required lay-verify="required" placeholder="请输入选项C" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input id="txtOptionD" type="text" value="@quest.OptionD" required lay-verify="required" placeholder="请输入选项D" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <div class="layui-btn-group" id="btnAnswers">
                @foreach (var s in list)
                {
                    string answer = (s == quest.Answer).ToString();
                    <button type="button" class="layui-btn layui-btn-sm" answer="@answer" onclick="sel(this)">
                        <i class="layui-icon">@s</i>
                    </button>
                }

                @*<button type="button" class="layui-btn layui-btn-sm @(quest.Answer=="A"?"layui-btn-normal":"")" onclick="sel(this)">
                        <i class="layui-icon">A</i>
                    </button>
                    <button type="button" class="layui-btn layui-btn-sm @(quest.Answer=="B"?"layui-btn-normal":"")" onclick="sel(this)">
                        <i class="layui-icon">B</i>
                    </button>
                    <button type="button" class="layui-btn layui-btn-sm @(quest.Answer=="C"?"layui-btn-normal":"")" onclick="sel(this)">
                        <i class="layui-icon">C</i>
                    </button>
                    <button type="button" class="layui-btn layui-btn-sm @(quest.Answer=="D"?"layui-btn-normal":"")" onclick="sel(this)">
                        <i class="layui-icon">D</i>
                    </button>*@
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">解析</label>
        <div class="layui-input-block">
            <input id="txtAnalysis" type="text" value="@quest.Analysis" required lay-verify="required" placeholder="解析" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="save();"><i class="layui-icon layui-icon-file-b"></i>保存</button>
            @if (quest.ID > 0)
            {
                <a href="@Url.Action("QuestEdit")" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-add-circle"></i>添加题目</a>
            }
        </div>
    </div>
</div>


@section script{
    <script>
        function save() {
            let obj = new Object();
            obj.ID = $("#txtID").val();
            obj.Title = $("#txtTitle").val();
            obj.OptionA = $("#txtOptionA").val();
            obj.OptionB = $("#txtOptionB").val();
            obj.OptionC = $("#txtOptionC").val();
            obj.OptionD = $("#txtOptionD").val();
            var answerbtn = $("#btnAnswers button[answer=true]");
            if (answerbtn.length==1) {
                obj.Answer=answerbtn.find("i").text();
            } else {
                obj.Answer = "";
            }
            obj.Analysis = $("#txtAnalysis").val();
            obj.Point = $("#txtPoint").val();
            let checked = true;
            if (obj.Title.length == 0) {
                checked = false;
                layer.tips('题目不能为空', '#txtTitle', {
                    tipsMore: true
                });
            }
            if (obj.OptionA.length == 0) {
                checked = false;
                layer.tips('选项A不能为空', '#txtOptionA', {
                    tipsMore: true
                });
            }
            if (obj.OptionB.length == 0) {
                checked = false;
                layer.tips('选项B不能为空', '#txtOptionB', {
                    tipsMore: true
                });
            }
            if (obj.OptionC.length == 0) {
                checked = false;
                layer.tips('选项C不能为空', '#txtOptionC', {
                    tipsMore: true
                });
            }
            if (obj.OptionD.length == 0) {
                checked = false;
                layer.tips('选项D不能为空', '#txtOptionD', {
                    tipsMore: true
                });
            }
            if (obj.Answer.length == 0) {
                checked = false;
                layer.tips('请选择答案', '#btnAnswers', {
                    tipsMore: true
                });
            }
            if (obj.Analysis.length == 0) {
                checked = false;
                layer.tips('解析不能为空', '#txtAnalysis', {
                    tipsMore: true
                });
            }
            if (obj.Point.length == 0) {
                checked = false;
                layer.tips('要点不能为空', '#txtPoint', {
                    tipsMore: true
                });
            }
            if (!checked) {
                //return;
            }
            layer.msg("正在保存，请稍候...", {
                icon: 16,
                time: 0,
                shade: 0.3,
                shadeClose: false
            });
            $.ajax({
                type: "POST",
                url: "@Url.Action("QuestSave")",
                data: obj,
                success: function (result) {
                    layer.msg(result.msg);

                },
                error: function (msg) {
                    layer.msg(msg);
                }
            });
        }

        function sel(obj) {
            $(obj).parent().find("button").each(function (i,ele) {
                $(ele).attr("answer","false");
            });
            $(obj).attr("answer", "true");
        }
    </script>
}
